/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			plgmdl.cc
  \date			July 2011
  \author		TNick

  \brief		Contains the implementation for PlgMdl class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n
 Please read ReadMe.txt and Licence.txt in root folder @n
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @n

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"plgmdl.h"

#include	<base/CC_App.h>
#include	<base/CC_PlugIn.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PlgMdl::PlgMdl	(QObject *parent) :
	QAbstractItemModel(parent)
{
	if (plgInCount() == 0)
		{
			p_env = NULL;
		}
	else
		{
			p_env = new PlgEnvel[plgInCount()];
			CC_PlugIn * p_plg =
					static_cast<CC_PlugIn *>(
						CC_App::pluginHeader()->first());
			int u_cnt = plgInCount();
			for (int i = 0; i < u_cnt; i++)
				{
					Q_ASSERT(p_plg != NULL);
					p_env[i].setPlgIn(p_plg);
					p_plg =
							static_cast<CC_PlugIn *>(
								p_plg->next());
				}
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
PlgMdl::~PlgMdl	(void)
{
	if (p_env != NULL)
		delete[] p_env;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QModelIndex		PlgMdl::index(int a_row, int a_column,
							  const QModelIndex &parent) const
{
	VirType *		p_ty;
	PlgEnvel *		p_lc_env;


	if ((a_row < 0) || (a_column < 0))
		{
			qDebug() << "  weird location";
			return QModelIndex();
		}

	/* diferentiate between root and leaf items */
	if (parent.isValid())
		{	/* this is a leaf */

			p_ty = static_cast<VirType *>(parent.internalPointer());
			if (p_ty->objectType() == PlgEnvel::TY_ENVELOPE)
				{
					p_lc_env = static_cast<PlgEnvel *>(p_ty);

					CC_PlugIn * p_plg =
							static_cast<CC_PlugIn *>(
								p_lc_env->plgInReveal());
					int cnt_cmd = (int)p_plg->countCmd();
					if (a_row >= cnt_cmd)
						return QModelIndex();

					return createIndex(a_row, a_column,
									   p_plg);
				}
			return QModelIndex();
		}
	else
		{	/* this is a root item */
			if (a_row >= plgInCount())
				return QModelIndex();
			return createIndex(a_row, a_column, &p_env[a_row]);
		}
	return QModelIndex();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QModelIndex		PlgMdl::parent (const QModelIndex &index) const
{
	VirType *		p_ty;
	PlgEnvel *		p_lc_env;
	int				i;
	CC_PlugIn *		plg;

	/* invalid items have no parent by definition :) */
	if (!index.isValid())
		{
			return QModelIndex();
		}
	p_ty = static_cast<VirType *>(index.internalPointer());
	if (p_ty == NULL)
		{
			return QModelIndex();
		}

	if (p_ty->objectType() != PlgEnvel::TY_PLUGIN)
		{ /* this is a plug-in (at top level( */
			return QModelIndex();
		}
	else /*if (p_ty->objectType() == PlgEnvel::TY_PLUGIN)*/
		{ /* this is a command */
			plg = static_cast<CC_PlugIn *>(p_ty);
			i = plg->index();
			p_lc_env = &p_env[i];
			return createIndex(i, 0, p_lc_env);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int				PlgMdl::rowCount	(const QModelIndex & parent) const
{
	VirType *		p_ty;
	PlgEnvel *		p_loc_env;
	CC_PlugIn *		plg;

	const QModelIndex * pp = &parent;

	/* diferentiate between root and leaf items */
	if (!parent.isValid())
		{	/* this is a root item */
			return plgInCount();
		}
	else
		{	/* this is a leaf */
			p_ty = static_cast<VirType *>(parent.internalPointer());
			if (p_ty == NULL)
				return 0;
			if (p_ty->objectType() == PlgEnvel::TY_ENVELOPE)
				{
					p_loc_env = static_cast<PlgEnvel *>(p_ty);
					plg = p_loc_env->plgInReveal();
					int i = plg->countCmd();
					return i;
				}
			else
				return 0;
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QVariant		PlgMdl::data		(
	const QModelIndex & midx, int role ) const
{
	VirType *	p_ty;
	PlgEnvel *	p_loc_env;
	CC_PlugIn *	p_plg;

	if (role != Qt::DisplayRole)
		return QVariant();
	if (!midx.isValid())
		return QVariant();

	p_ty = static_cast<VirType *>(midx.internalPointer());
	if (p_ty == NULL)
		return QVariant();
	if (p_ty->objectType() == PlgEnvel::TY_ENVELOPE)
		{ /* the envelope contains the plug-in */
			p_loc_env = static_cast<PlgEnvel *>(p_ty);
			p_plg = p_loc_env->plgInReveal();
			return QVariant(p_plg->Name());
		}
	else if (p_ty->objectType() == PlgEnvel::TY_PLUGIN)
		{ /* the plug-in is the header for command */
			p_plg = static_cast<CC_PlugIn *>(p_ty);
			/* todo*/
			return QVariant("command");
		}
	else
		{
			return QVariant();
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QVariant		PlgMdl::headerData		(
	int section, Qt::Orientation orientation, int role) const
{
	Q_UNUSED(orientation);

	if (role != Qt::DisplayRole)
		return QVariant();
	if (section != 0)
		return QVariant();
	return QVariant("Name");

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
int				PlgMdl::plgInCount		(void) const
{
	return CC_App::pluginHeader()->count();
}
/* ========================================================================= */

/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */

